var phone = document.getElementById("phone");
var pwd = document.getElementById("code");
var btn = document.getElementById("btna");
var maincode = document.getElementById("maincode");
var phoneReg = /^1[0-9]{10}$/;
var pwdReg = /^(\w){6,20}$/;

var wait = 60;

function time(o) {
	if (wait == 0) {
		maincode.disabled = false;
		o.textContent = "获取验证码";
		wait = 60;
	} else {
		maincode.disabled = true;
		o.textContent = "重新发送(" + wait + ")";
		wait--;
		setTimeout(function() {
			time(o)
		}, 1000)
	}
}

function yanzhengma() {
	if (phone.value != '') {
		if (!phoneReg.exec(phone.value)) {
			mui.toast('手机号填写不正确');
			phone.value = '';
		} else {
			var maincode = document.getElementById("maincode");
			var jsons = "account=" + phone.value + "&smsType=1";
			var codeUrl = getUrl('checkCode');
			mui.ajax(codeUrl, {
				data: jsons,
				dataType: 'json',
				type: 'post',
				timeout: 3000,
				success: function(data) {
					time(maincode);
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					console.log('获取验证码失败');
				}
			});
		}
	}
}

function login() {
	var jsons = "account=" + phone.value + "&vcode=" + pwd.value;
	var loginUrl = getUrl("loginCode");
	plus.nativeUI.showWaiting('正在登录...');
	mui.ajax(loginUrl, {
		data: jsons,
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 3000, //超时时间设置为10秒；
		success: function(data) {
			//服务器返回响应，根据响应结果，分析是否登录成功；			
			if (data.code == 0) {
				localStorage.account = writeData(data.obj.accountId);
				localStorage.nickName = writeData(data.obj.nickName);
				localStorage.phone = writeData(data.obj.account);
				localStorage.token = writeData(data.obj.token);
				localStorage.tokenExpire = writeData(data.obj.tokenExpire);
				localStorage.huanId = writeData(data.obj.huanId);
				refresh();
				setTimeout(function() {
					plus.webview.getWebviewById('loginHead.html').close();
					plus.nativeUI.closeWaiting();
					plus.webview.currentWebview().opener().close();
				}, 1000)
			} else {
				plus.nativeUI.closeWaiting();
				mui.toast(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			plus.nativeUI.closeWaiting();
			console.log('登录失败');
		}
	});
}
//刷新
function refresh() {
	reloadHtml("home.html", "personal.html", "system.html", "guanzhu.html", "market.html");
}

function closeWin() {
	plus.webview.currentWebview().opener().close();
}